import Yaml from 'yaml';

const I18n = {
    __cache__: {},
    load(locale) {
        if (this.__cache__[locale]) {
            return new Promise(resolve => {
                if (this.__cache__[locale]) {
                    resolve(this.__cache__[locale]);
                }
            });
        }


        let path = "./i18n/" + locale + ".yaml";
        return fetch(path)
            .then(res => res.text())
            .then(str => Yaml.parse(str))
            .then(message => {
                this.__cache__[locale] = message;
                return message;
            });

        // return new Promise( (resolve,reject) => {
        //     if(this.__cache__[locale]){
        //         resolve(this.__cache__[locale]);
        //         return;
        //     }
        //
        //
        //     let file = Fs.readFileSync(path,'utf-8');
        //     if(file){
        //         this.__cache__[locale] = Yaml.parse(file);
        //         resolve(this.__cache__[locale]);
        //     }else{
        //         reject(new Error('Cannot read file['+path+']'));
        //     }
        // })
    }
}

export default I18n;
